package com.fg.cloud.framework.shiro;

import java.util.Set;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.CollectionUtils;
import org.apache.shiro.web.filter.authz.AuthorizationFilter;

import com.fg.cloud.common.shiro.ShiroUser;


/**
 * Description:自定义权限校验
 * @author around
 * @date 2017年9月29日上午9:44:43
 */
public class AnyRolesAuthorizationFilter extends AuthorizationFilter {

	@Override
	protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue)
			throws Exception {
		Subject subject = getSubject(request, response);
		String[] rolesArray = (String[]) mappedValue;
		ShiroUser user = (ShiroUser) subject.getPrincipals().getPrimaryPrincipal();
		
		//没有角色校验
		if (rolesArray == null || rolesArray.length == 0) {
			return true;
		}
		
		Set<String> roles = CollectionUtils.asSet(rolesArray);
		for (String role : roles) {
			if (subject.hasRole(role)) {
				return true;
			}
		}
		return false;
	}

}
